<?php

namespace Home\Controller;

use Think\Controller;

class CommonQuestionController extends Controller {

    public function index() {
        $this->display("CommonQuestion");
    }

    /**
     * 常见问题列表获取
     */
    public function getQuestions() {
        $questionsObj = M('commonquestion');
        $condition["PublicCodeID"] = 1;
        $questionList = $questionsObj->where($condition)->order('ID desc')->select();
        if (count($questionList)) {
            $json = json_encode($questionList, JSON_UNESCAPED_UNICODE);
            $this->ajaxReturn($json, 'EVAL');
        } else {
            $this->ajaxReturn('[]', 'EVAL');
        }
    }

    public function addQuestion() {
        
        $questionsObj = M('commonquestion');
        $question = I('question', '', 'htmlspecialchars');
        $answer = I('answer', '', 'htmlspecialchars');
        $data['Question'] = $question;
        $data['Answer'] = $answer;
        $data['PublicCodeID'] = 1;
        $result= $questionsObj->add($data);
        if ($result) {
            $this->ajaxReturn('y', 'EVAL');
        }
        else{
            $this->ajaxReturn('n', 'EVAL');
        }
    }

    public function updateQuestion() {
        $questionsObj = M('commonquestion');
        $questionID = I('questionId', '', 'htmlspecialchars');
        $question = I('question', '', 'htmlspecialchars');
        $answer = I('answer', '', 'htmlspecialchars');
        $data['Question'] = $question;
        $data['Answer'] = $answer;
        $data['PublicCodeID'] = 1;
        $result= $questionsObj->where('id=' . $questionID)->save($data);
         if ($result) {
            $this->ajaxReturn('y', 'EVAL');
        }
        else{
            $this->ajaxReturn('n', 'EVAL');
        }
    }

    public function deleteQuestion() {
        $questionID = I('questionId', '', 'htmlspecialchars');
        $questionsObj = M('commonquestion');
        $condition["PublicCodeID"] = 1;
        $condition["ID"] = $questionID;
        $questionsData = $questionsObj->where($condition)->delete(); 
         if ($questionsData) {
            $this->ajaxReturn('y', 'EVAL');
        }
        else{
            $this->ajaxReturn('n', 'EVAL');
        }
    }

}
